package com.cndw.kungfu.model.disciple;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsAct;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.disciple.domain.DiscipleRein;

/**
 * 玩家门派数据集合
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-7-1 上午11:30:55
 */
public class PlayerMartial {

	private int reinId = 0;
	private Map<Integer, List<DiscipleRein>> reinBuffer = new HashMap<Integer, List<DiscipleRein>>();
	/**
	 * 徒弟列表
	 */
	private Map<Integer, Disciple> disciples;

	public PlayerMartial(Map<Integer, Disciple> disciples, Map<Integer, List<DiscipleRein>> reinBuffer) {
		this.reinBuffer = reinBuffer;
		this.disciples = disciples;
	}

	public Collection<Disciple> getDisciples() {
		return disciples.values();
	}

	public int getScore() {
		int score = 0;
		for (Disciple disciple : disciples.values()) {
			if (disciple.getCampId() == 0) {
				continue;
			}
			score += disciple.getScore();
		}
		return score;
	}

	public Disciple getDiscipleById(int id) {
		return disciples.get(id);
	}

	public Disciple getDisciple(int discipleId) {
		for (Disciple e : disciples.values()) {
			if (e.getDiscipleId() == discipleId) {
				return e;
			}

		}
		return null;
	}

	public boolean addDisciple(Disciple disciple) {
		if (disciple == null) {
			return false;
		}
		disciples.put(disciple.getId(), disciple);
		return true;
	}

	public void delDisciple(int id) {
		disciples.remove(id);
	}

	public List<DiscipleRein> getReins() {
		int type = 0;
		if (!reinBuffer.containsKey(type)) {
			reinBuffer.put(type, new LinkedList<DiscipleRein>());
		}
		return reinBuffer.get(type);
	}

	private synchronized int makeId() {
		reinId++;
		reinId = reinId >= Constants.commIntSignMax ? 1 : reinId;
		return reinId;
	}

	public void reinBuffer(DiscipleRein discipleRein) {
		int reinId = makeId();
		discipleRein.setId(reinId);
		List<DiscipleRein> currentReins = getReins();
		if (currentReins.size() >= 10) {
			currentReins.remove(0);
		}
		currentReins.add(discipleRein);
	}

	public List<DiscipleRein> getBufferReins() {
		return getReins();
	}
	
	public int findSpcSign(int sign){
		int currt = 0 ;
		for (Disciple e : disciples.values()) {
			if (e.getIcon() == ConstantsAct.spcDiscipleBase * 1 + sign) {
				currt |= 1;
			}else if (e.getIcon() == ConstantsAct.spcDiscipleBase * 2 + sign) {
				currt |= 2;
			}
			if (currt == 3) {
				return currt;
			}
		}
		return currt;
	}

}
